package com.example.springbootdemo.api.controller;

import com.example.springbootdemo.api.DTO.GetScoreDTO;
import com.example.springbootdemo.app.service.ScService;
import com.example.springbootdemo.domin.VO.getScoreVO;
import com.example.springbootdemo.domin.entity.Sc;
import com.example.springbootdemo.infra.util.Result;
import com.example.springbootdemo.infra.util.ScoreException;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/sc")
@Api(value = "成绩相关controller", tags = {"成绩相关接口"})
public class ScController {
    @Autowired
    private ScService scService;
    private final Logger logger = LoggerFactory.getLogger(ScController.class);

    /**
     * 根据学号或者课程号查询单科成绩
     */
    @PostMapping("/getScoreBySnoAndCno")
    public Result<String> getScoreBySnoOrCno(@RequestBody GetScoreDTO getScoreDTO) {
        logger.info("学号或课程号为", getScoreDTO);
        String scoreByTnoOrCno = scService.getScoreByTnoOrCno(getScoreDTO);
        return Result.success(scoreByTnoOrCno);
    }

    /**
     * 根据学号查询该学生所有成绩
     *
     * @return
     */
    @GetMapping("/allScore/{sno}")
    public Result<List<getScoreVO>> getScoreBySno(@PathVariable Integer sno) {
        if (sno == null) {
            throw new ScoreException();//todo抛出异常
        }
        logger.info("查询的学生成绩的学号为:{}", sno);
        List<getScoreVO> score = scService.getScore(sno);
        return Result.success(score);
    }

    /**
     * 修改成绩
     */
    @PostMapping("/updateScore")
    public ModelAndView updateScore(Sc sc) {
        logger.info("查询的学生信息为:{}", sc);
        scService.updateScore(sc);
        List<Sc> all = scService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("sc");
        modelAndView.addObject("sc", all);
        return modelAndView;
    }

    /**
     * 根据成绩单编号获取成绩
     */
    @GetMapping("/getById/{scoreno}")
    public ModelAndView getById(@PathVariable("scoreno") Integer scoreno) {
        Sc score = scService.getById(scoreno);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("change_sc");
        modelAndView.addObject("sc", score);
        return modelAndView;
    }


    /**
     * 增加学生某们课程成绩
     */
    @PostMapping("/addScore")
    public ModelAndView addScore(Sc sc) {
        scService.addScore(sc);
        List<Sc> all = scService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("sc");
        modelAndView.addObject("sc", all);
        return modelAndView;
    }

    /**
     * 删除某个学生的成绩
     */
    @PostMapping("/deleteScore")
    public Result deleteScore(@RequestBody GetScoreDTO getScoreDTO) {
        scService.deleteScore(getScoreDTO);
        return Result.success();
    }

    @GetMapping("/getAll")
    public ModelAndView getAll() {
        List<Sc> all = scService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("sc");
        modelAndView.addObject("sc", all);
        return modelAndView;
    }

    @GetMapping("/delete/{scoreno}")
    public ModelAndView deleteBy(@PathVariable("scoreno") Integer scoreno) {
        if (scoreno == null) {
            //throw new Exception();todo
        }
        scService.delete(scoreno);
        List<Sc> all = scService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("sc");
        modelAndView.addObject("sc", all);
        return modelAndView;
    }


}
